<script type="text/javascript">
	$(function(){
		$('.circs').click(function(){
			$('.vote_opts').slideToggle('slow');
		});

		//提取所有投票的详情
		vote_id = "<php>$ids = implode(',', $ids);echo $ids;</php>";
		$.post("{:U('home/Vote/getVotesDetail')}", {ids:vote_id}, function(result) {
			if(result == '""') {
				$('#vote_add_vote').show(); //显示“发起投票”按钮
				return ;
			}
			result = eval("("+result+")");
			for(var one in result){
				$('#vote_detail_'+one).html(result[one]);
				updateData(one);
				$('#vote_LogList').prepend('<input type="hidden" name="vote_id" value="'+one+'"/>');
			}
		});
	});

	//发起投票
	function addVote() {
		$.tbox.popup("<php>echo U('home/Vote/add', 'type=2');</php>",'{:L(vote_add)}',{ok:"doAddVote"});
	}

	function check_add_vote() {
		var tit = $.trim($("#vote_title").val());
		if(tit == ''){
			$.tbox.no('{:L(vote_check_title)}', '{:L(vote_hint)}');
			setTimeout("$.tbox.close()",1500);
			return false;
		}
		var flag = 0;
		$(".vote_opt").each(function (i) {
			if($.trim($(this).val())) flag++;
		});
		if(flag<2){
			$.tbox.no('{:L(vote_check_opt)}', '{:L(vote_hint)}');
			setTimeout("$.tbox.close()",1500);
			return false;
		}
		var typ = $( '#vote_type' ).val();
		if( typ > flag-1 ) {
			$.tbox.no('{:L(vote_check_opt_num)}', '{:L(vote_hint)}');
			setTimeout("$.tbox.close()",1500);
			return false;
		}
		return true;
	}

	//添加投票
	function doAddVote() {
		if(check_add_vote() == false) {
			return false;
		}
		var app     = "<php>echo $app;</php>";
		var field   = "<php>echo $field;</php>";
		var fieldId = "<php>echo $fieldId;</php>";
		var groupId = "<php>echo $groupId;</php>";
		var tit   = $('#vote_title').val();
		var exp   = $('#vote_explain').val();
		var typ   = $('#vote_type').val();
		var d_typ = $(".deadline_type:checked").val();
		var year  = $('#vote_year').val();
		var month = $('#vote_month').val();
		var day   = $('#vote_day').val();
		var anony = $(".is_anonymous:checked").val();
		var opt   = new Array();
		$("#addVoteForm :text").each(function(i, n){
			if(i != 0) {
				opt.push($(this).val());
			}
		});
		
		$.post("{:U('home/Vote/doAddVote')}", {isAjax:1, app:app, field:field, fieldId:fieldId, groupId:groupId, title:tit, explain:exp, type:typ, deadline_type:d_typ, year:year, month:month, day:day, is_anonymous:anony, opt:opt}, function(result) {
			if(result >= 1) {
				$.tbox.yes('{:L(vote_save_success)}','{:L(vote_hint)}');
				setTimeout("$.tbox.close()",1500);

				var li = $('#vote_clone').clone(true);
				li.insertAfter('#vote_clone');
				li.attr('id','vote_list_'+result).show();
				li.find('.style_1').attr('id', 'vote_summary_'+result);
				li.find('.tips').attr('href', "{:U('space/View/index','uid='.$mid)}");
				li.find('img').attr('src', "{$mid|getUserFace}");
				li.find('.U').attr('href', "{:U('space/View/index','uid='.$mid)}");
				li.find('.U').html("{$mid|getUserName}");
				li.find('strong').html(tit);
				if(typ <= 0) {
					var check_type = 'radio';
				}else {
					var check_type = 'checkbox';
				}
				li.find('.box .style_1 .c2 h3').after('<p><input type="'+check_type+'" disabled="true" /> '+opt[0]+'</p><p><input type="'+check_type+'" disabled="true"/> '+opt[1]+'</p>');
				li.find('.box .style_1 .c2 p span').html('{:L(vote_cTime)}：{:L(vote_now)}&nbsp;&nbsp; {:L(vote_end_Time)}：2011-01-01');
				li.find('.box .style_1 .c3').append('<a class="add" href="javascript:void(0);" onclick="show_vote_detail('+result+')">{:L(vote_see)}</a>');
				li.find('.style_2').attr('id', 'vote_detail_'+result);
				
				//投票详情
				$.post("{:U('home/Vote/getVotesDetail')}", {ids:result}, function(result_detail) {
					if(result_detail != '""') {
						$('#vote_add_vote').hide(); //隐藏“发起投票”按钮
					}
					result_detail = eval("("+result_detail+")");
					for(var one in result_detail){
						$('#vote_detail_'+one).html(result_detail[one]);
					}
					show_vote_detail(result);
					$('#vote_LogList').prepend('<input type="hidden" name="vote_id" value="'+result+'"/>');
				});
			}else if(result == -1) {
				$.tbox.no('{:L(vote_check_endTime)}','{:L(vote_hint)}');
				setTimeout("$.tbox.close()",1500);
			}else if(result == -2) {
				$.tbox.no('{:L(vote_check_repeat)}','{:L(vote_hint)}');
				setTimeout("$.tbox.close()",1500);
			}else {
				$.tbox.no('{:L(vote_save_error)}','{:L(vote_hint)}');
				setTimeout("$.tbox.close()",1500);
			}
		});
	}

	//管理投票
	function vote_admin(vid, typ) {
		if(typ == 1) {
			//添加选项
			html  = '<div style="padding:10px;">{:L(vote_new_opt)}：<input class="in_put" type="text" id="new_opt_name" value="" /></div>';
			html += '<input type="hidden" id="vote_id" value="'+vid+'"/>';
			$.tbox.popBox(html,'{:L(vote_add_opt)}',{ok:"doAddOpt"});
		}else if(typ == 2) {
			//修改结束时间
			html  = $('#new_vote_deadline').html();
			html += '<input type="hidden" id="vote_id" value="'+vid+'"/>';
			$.tbox.popBox(html,'{:L(vote_add_opt)}',{ok:"doUpdateDeadline"});
		}else if(typ == 3) {
			//删除投票
			if (confirm('{:L(vote_delete)}')) {
				$.post("{:U('home/Vote/delVote')}", {id:vid}, function(res) {
					if(res == 1) {
						$('#vote_list_'+vid).hide("slow");
						$('#vote_list_'+vid).remove();
						$('#vote_add_vote').show(); //显示“发起投票”按钮
						$.tbox.yes('{:L(vote_delete_success)}','{:L(vote_hint)}');
						setTimeout("$.tbox.close()",1500);
					}else {
						$.tbox.no('{:L(vote_delete_error)}','{:L(vote_hint)}');
						setTimeout("$.tbox.close()",1500);
					}
				});
			}
		}
	}

	//添加选项
	function doAddOpt() {
		var opt = $('#new_opt_name').val();
		if($('#new_opt_name').val() == '') {
			$.tbox.no('{:L(vote_check_opt_empty)}', '{:L(vote_hint)}');
			setTimeout("$.tbox.close()",1500);
			return;
		}
		var vid = $('#vote_id').val();
		$.post("{:U('home/Vote/doAddOpt')}", {id:vid, title:opt}, function(res) {
			if(res == 0) {
				$.tbox.no('{:L(vote_add_error)}','{:L(vote_hint)}');
				setTimeout("$.tbox.close()",2500);
			}else if(res == -1) {
				$.tbox.no('{:L(vote_opt_has_exist)}', '{:L(vote_hint)}');
				setTimeout('$.tbox.close()',2500);
			}else {
				$.tbox.yes('{:L(vote_add_success)}','{:L(vote_hint)}');
				setTimeout("$.tbox.close()",1500);
				temp = res.split(',');
				var num		  = temp[0]; //第几位（从0开始）
				var opt_id	  = temp[1]; //选项ID
				var typ		  = (temp[2] == 0) ? 'radio' : 'checkbox'; //投票的类型：单选or多选
				var has_v	  = temp[3]; //是否已投票
				html  = '<li><div style="margin-left: 20px; width: 20px;" class="left lh25">'+(parseInt(num)+1)+'. </div>';
				html += '<div style="width: 80%;" class="alL left lh25 ">'+opt+': </div>';
				html += '<div style="width: 204px; margin-left: 30px;" class="left"><div class="poll">';
				html += '<div style="width: 0%; display: block;" id="vote_'+vid+'_opt_'+num+'_per" class="vbg v1 vote-per-'+num+'"> <span/> </div>';
				html += '</div></div>';
				html += '<div style="width: 81px;" id="vote_'+vid+'_opt_'+num+'_static" class="left">0 / 0%</div>';
				if(has_v == 0) {
					html += '<div style="width: 30px;" class="left vote_'+vid+'_check"><input type="'+typ+'" id="'+opt_id+'" value="'+opt+'" name="vote_opt"/></div>';
				}
				html += '<div class="C"></div>';
				$('#vote_'+vid+'_opt').append(html);
			}
		});
		$('#new_vote_opt').html('<div style="padding:10px;">{:L(vote_new_opt)}：<input class="in_put" type="text" id="new_opt_name" value="" /></div>');
	}

	//修改结束时间
	function doUpdateDeadline() {
		html = $('#new_vote_deadline').html();
		$('#new_vote_deadline').html('');
		var vid   = $('#vote_id').val();
		var typ   = $(".update_deadline_type:checked").val();
		var year  = $('#update_vote_year').val();
		var month = $('#update_vote_month').val();
		var day   = $('#update_vote_day').val();
		$.post("{:U('home/Vote/doUpdateDeadline')}", {id:vid, type:typ, year:year, month:month, day:day}, function(res){
			if(res == -1) {
				$.tbox.no('{:L(vote_check_endTime)}', '{:L(vote_hint)}');
				setTimeout("$.tbox.close()",1500);
			}else if(res == 0) {
				$.tbox.no('{:L(vote_save_error)}', '{:L(vote_hint)}');
				setTimeout("$.tbox.close()",1500);
			}else {
				$('#vote_deadline_1').html('{:L(vote_end_Time)}：'+res);
				$('#vote_deadline_2').html('{:L(vote_end_Time)}：'+res);
				$.tbox.yes('{L(vote_add_success)}','{:L(vote_hint)}');
				setTimeout("$.tbox.close()",1500);
			}
		});
		$('#new_vote_deadline').html(html);
	}

	//参与投票
	function post_vote(vid, max) {
		if(max == 1) {
			var opts = $('#vote_list_'+vid+' input[name=vote_opt]').filter( '[checked]' ).val();
			var opts_ids = $('#vote_list_'+vid+' input[name=vote_opt]').filter( '[checked]' ).attr("id");
		}else {
			var opts = '';
			var opts_ids = '';
			var num = 0;
			$('#vote_list_'+vid+' input[type=checkbox]').filter( '[checked]' ).each(function(i){
				opts = opts + $(this).val()+",";
				opts_ids = opts_ids + $(this).attr("id")+",";
				num = num + 1;
			});

			if(num > max){
				$.tbox.no("{L:(vote_more_vote)}"+max+"{:L(vote_opt)}", '{:L(vote_hint)}');
				setTimeout("$.tbox.close()",2500);
				return false;
			}
		}

		if(!opts) {
			$.tbox.no('{:L(vote_num_one_opt)}', '{:L(vote_hint)}');
			setTimeout("$.tbox.close()",2500);
			return false;
		}

		$.post("{:U('home/Vote/postVote')}",{vote_id:vid,opts:opts,opts_ids:opts_ids},function(txt){
			if(txt){
				if( -2 == txt ){
					$.tbox.no('{:L(vote_has_over)}', '{:L(vote_hint)}');
					setTimeout("$.tbox.close()",2500);
				}else if( -1 == txt ){
					$.tbox.no('{:L(vote_has_opt)}', '{:L(vote_hint)}');
					setTimeout("$.tbox.close()",2500);
				}else{
					updateData(vid);
					updateUsers(vid);
					$('.vote_'+vid+'_check').remove();
					$('#vote_detail_'+vid).find("input[value='{:L(vote_name)}']").val('{:L(vote_has_opt)}').attr('disabled', true);
				}
			}
		});
	}

	//显示投票详情
	function show_vote_detail(vid) {
		$('#vote_summary_'+vid).hide("fast");
		$('#vote_detail_'+vid).show("fast");
		updateData(vid);
		updateUsers(vid);
	}

	//隐藏投票详情
	function hide_vote_detail(vid) {
		$('#vote_summary_'+vid).show("fast");
		$('#vote_detail_'+vid).hide("fast");
	}

	//更新进度条
	function updateData(vid) {
		$.post("{:U('home/Vote/getOptPercent')}", {id:vid}, function(res) {
			if(res == 0) {
				$.tbox.no('{:L(vote_data_error)}','{:L(vote_hint)}');
				setTimeout("$.tbox.close()",1500);
				return;
			}
			res = eval("("+res+")");
			var num = 0;
			for(var one in res) {
				if(one == 'num_of_users') {
					$('#vote_list_'+vid+'_num').html(res[one]+'人');
				}else {
					temp = res[one].split(';');
					$('#vote_'+vid+'_opt_'+one+'_static').html(temp[0] + ' / ' + temp[1] + '%');
					$('#vote_'+vid+'_opt_'+one+'_per').animate({width:temp[1] + '%'}, 2000);
					num += parseInt(temp[0]);
				}
			}
			$('#vote_'+vid+'_num').html('{:L(vote_num_opt)}：' + num);
		});
	}

	//更新参与投票的用户
	function updateUsers(vid, limit_b, limit_e) {
		$.post("{:U('home/Vote/getVoteUsers')}", {id:vid, limit_b:limit_b, limit_e:limit_e}, function(users) {
			if(users == 0) {
				$.tbox.no('{:L(vote_data_error)}','{:L(vote_hint)}');
				setTimeout("$.tbox.close()",1500);
				return ;
			}else if(users == 'is_anonymous') {
				$('#circs_vote_'+vid).html('<dl><dt>{:L(vote_anonymous)}</dt></dl><div class="c"></div>');
				return ;
			}

			users = eval("("+users+")");
			var now_uid = "<php>echo $mid;</php>";
			var flag    = 0; //投票的人数
			html = '';
			for(var i in users) {
				if(users[i]['id'] == undefined) continue;
				html += '<div class="left mt5" style="width: 20px;"> ';
				if(now_uid == users[i]["uid"]) {
					html += '<img src="__THEME__/images/arrow_y.gif"/>';
				}
				html += '</div>';
				html += '<div class="left" style="width: 580px;"><a target="_blank" href="__ROOT__/space/'+users[i]['uid']+'">'+users[i]['name']+'</a> '+users[i]['cTime']+' {:L(vote_who)}“'+users[i]['opts']+'”</div><div class="C"></div>';
				flag ++;
			}
			if(limit_b != undefined && limit_e != undefined) {
				if(flag <= 0) {
					$.tbox.yes('{:L(vote_not_more_user)}','{:L(vote_hint)}');
					setTimeout('$.tbox.close()', 1500);
					return ;
				}
				$('#vote_'+vid+'_more_users').before(html);
				limit_b = parseInt(limit_b);
				limit_e = parseInt(limit_e);
				limit_b = limit_b + limit_e;
				$('#vote_'+vid+'_more_users').html('<div class="left mt5" style="width: 20px;"> </div><a href="javascript:void(0)" onclick="updateUsers('+vid+','+limit_b+','+limit_e+')">[{:L(vote_more)}]</a>');
			}else {
				if(flag <= 0) {
					html = '<div style="width: 580px;" class="left">{:L(vote_no_have_user)}</div>';
					$('#vote_'+vid+'_user').html(html);
					return ;
				}
				$('#vote_'+vid+'_user').html(html);
			}
		});
	}

	//收起/展开投票用户
	function vote_show_hide_users(vid) {
		if($('#vote_show_hide_users').html() == '{:L(vote_slideUp)}') {
			$('#vote_'+vid+'_user').slideUp('slow');
			$('#vote_show_hide_users').html('{:L(vote_slideDown)}');
		}else if($('#vote_show_hide_users').html() == '{:L(vote_slideDown)}') {
			$('#vote_'+vid+'_user').slideDown('slow');
			$('#vote_show_hide_users').html('{:L(vote_slideUp)}');
		}
	}
</script>

	<eq name="type" value="edit">
    <div class="page_title" style="display:none" id="vote_add_vote">
		<a href="javascript:void(0);" onclick="addVote();">{:L('vote_add')}</a>
	</div>
	</eq>

    <div class="pollBox">
      <div class="LogList" id="vote_LogList">
        <ul id="vote_list">
		  <!-- CloneVote begin -->
		  <li id="vote_clone" style="display:none;">
		    <div class="box">
		  	    <div class="style_1" id="0">
				  <div class="c1"> 
					<span class="headpic50"> <a href="#"  class="tips" rel="#" target="_blank"><img src="{$mid|getUserFace}</a> </span> 
					<a href="#" class="U" target="_blank"></a> 
				  </div>					
				  <div class="c2">
				    <h3 class="f14"><strong></strong></h3>
				    <p><span class="right cl06">&nbsp</span>．．．</p>
				  </div>
				  <div class="c3">
				    <p class="cl06"><span class="f18px fB">0{:L(vote_human)}</span>{:L(vote_join_vote)}</p>
				    <br />
				    <br />
				  </div>
			    </div>
			    <div class="C"></div>

			    <!-- 投票详情 begin -->
			    <div class="style_2" id="0" style="display:none;">
			    </div>
			    <!-- 投票详情 end -->
		    </div>
		  </li>
		  <!-- CloneVote end -->

		  <!-- 投票列表 begin -->
          <volist name="data" id="vo">
            <li id="vote_list_{$vo['id']}">
              <div class="box">
				  <div class="style_1" id="vote_summary_{$vo['id']}" style="display:none;">

					<div class="c1"> 
						<span class="headpic50"> <a href="#"  class="tips" rel="#"><img src="{$vo.uid|getUserFace}" target="_blank"/></a> </span> 
						<a href="#" class="U" target="_blank">{$vo.name}</a> 
					</div>

					<div class="c2">
					  <h3 class="f14"><strong>{$vo.title}</strong></h3>
					  <volist name="vo.opts" id="opt">
						<php>if($vo['type'] == "0"){</php>
							<p><input type="radio" name="checkbox" id="checkbox" disabled="true"/>
						<php>}else{</php>
							<p><input type="checkbox" name="checkbox" id="checkbox" disabled="true"/>
						<php>}</php>
						{$opt.name}</p>
					  </volist>

					  <php>if( $vo['deadline'] < time() ) {</php>
						  <p><span class="right">{:L(vote_who_add, array('users'=>"<a href='__TS__/space/$vo[uid]'>getUserName($vo[uid])</a>"))}</span><span class="cRed">{:L(vote_has_over)}</span></p>
					  <php>}else {</php>
						  <p><span class="right cl06">{:L(vote_cTime)}：{$vo.cTime|friendlyDate}&nbsp;&nbsp; <span id="vote_deadline_1">{:L(vote_end_Time)}：{$vo.deadline|date="Y-m-d H:i:s",###}</span></span>．．．</p>
					  <php>}</php>
					</div>

					<div class="c3">
					  <p class="cl06"><span class="f18px fB" id="vote_list_{$vo['id']}_num">{$vo.vote_num}{:L(vote_human)}</span>{:L(vote_join_vote)}</p>
					  <br />
					  <br />
					  <a class="add" href="javascript:void(0);" onclick="show_vote_detail({$vo['id']});">{:L(vote_see)}</a> 
					</div>
				  </div>

				  <div class="C"></div>


				  <!-- 投票详情 begin -->
				  <div class="style_2" id="vote_detail_{$vo['id']}">
					  
				  </div>
				  <!-- 投票详情 end -->
              </div>
            </li>
          </volist>
		  <!-- 投票列表 end -->
        </ul>
        <div id="Pagination" class="pagination">{$html}</div>
      </div>
      <!-- LogList end  -->
    </div>
  <div class="C"></div>


  <!-- 弹窗 修改结束时间 -->
  <div id="new_vote_deadline" style="display:none;">
    <div style="padding:10px; line-height:180%">{:L(vote_new_availability)}<br />
		<php>
			$now_year  = date('Y');
			$now_month = date('m');
			$now_day   = date('d');
		</php>
		<input class="update_deadline_type" type="radio" name="deadline_type" id="deadline_type_7" value="7" checked="checked"/> {:L(vote_one_week)}<br /> 
		<input class="update_deadline_type" type="radio" name="deadline_type" id="deadline_type_30" value="30" /> {:L(vote_one_month)} <br /> 
		<input class="update_deadline_type" type="radio" name="deadline_type" id="deadline_type_u" value="u" /> {:L(vote_custom)} <!-- TODO 灰色 -->
		<select name="year" name="deadline_type" id="update_vote_year" class="in_put_01">
			<option value="{$now_year}">{$now_year}</option>
			<option value="<php>echo $now_year+1;</php>"><php>echo $now_year+1;</php></option>
			<option value="<php>echo $now_year+2;</php>"><php>echo $now_year+2;</php></option>
		</select>
		{:L(vote_year)}
		<select name="month" id="update_vote_month" class="in_put_01">
			<php>$month = array(1,2,3,4,5,6,7,8,9,10,11,12);</php>
			<volist name="month" id="vo">
				<option value="{$vo}" <php>if($vo==$now_month){</php>selected="selected"<php>}</php>>{$vo}</option>
			</volist>
		</select>
		{:L(vote_month)}
		<select name="day" id="update_vote_day" class="in_put_01">
			<php>$day = array(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31);</php>
			<volist name="day" id="vo">
				<option value="{$vo}" <php>if($vo==$now_day){</php>selected="selected"<php>}</php>>{$vo}</option>
			</volist>
		</select>
		{:L(vote_day)}
    </div>
  </div>